package com.chkj.fbook.system.controller;

import java.util.List;
import java.util.Map;

import com.chkj.fbook.common.utils.R;
import com.chkj.fbook.system.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.chkj.fbook.common.annotation.Log;
import com.chkj.fbook.common.annotation.Permission;
import com.chkj.fbook.common.controller.BaseController;
import com.chkj.fbook.common.domain.Tree;
import com.chkj.fbook.system.domain.MenuDO;

/**
 * @author bootdo 980027164@qq.com
 */
@RequestMapping("/sys/menu")
@Controller
public class MenuController extends BaseController {

	String prefix = "system/menu";

	@Autowired
	private MenuService menuService;

	@Log("菜单权限-去到列表页面")
	@Permission(name = "sys:menu:menu")
	@GetMapping()
	public String menu(Model model) {
		return prefix + "/menu";
	}

	@Log("菜单权限-获取列表信息")
	@Permission(name = "sys:menu:menu")
	@RequestMapping("/list")
	@ResponseBody
	public List<MenuDO> list(@RequestParam Map<String, Object> params) {
		List<MenuDO> menus = menuService.list(params);
		return menus;

	}

	@Log("菜单权限-去到新增页面")
	@Permission(name = "sys:menu:add")
	@GetMapping("/add/{pId}")
	public String add(Model model, @PathVariable("pId") String pId) {
		model.addAttribute("pId", pId);
		if ("0".equals(pId)) {
			model.addAttribute("pName", "根目录");
		} else {
			model.addAttribute("pName", menuService.get(pId).getName());
		}
		return prefix + "/add";
	}

	@Log("菜单权限-去到修改页面")
	@Permission(name = "sys:menu:edit")
	@GetMapping("/edit/{id}")
	public String edit(Model model, @PathVariable("id") String id) {
		MenuDO mdo = menuService.get(id);
		String pId = mdo.getParentId();
		model.addAttribute("pId", pId);
		if ("0".equals(pId)) {
			model.addAttribute("pName", "根目录");
		} else {
			model.addAttribute("pName", menuService.get(pId).getName());
		}
		model.addAttribute("menu", mdo);
		return prefix + "/edit";
	}

	@Log("菜单权限-保存操作")
	@Permission(name = "sys:menu:add")
	@PostMapping("/save")
	@ResponseBody
	public R save(MenuDO menu) {
		
		if ("0".equals(menu.getParentId())) {
			return R.error(1, "不能直接新增上级菜单为根目录的系统权限！");
		}
		
		if (menuService.save(menu) > 0) {
			return R.ok();
		} else {
			return R.error(1, "保存失败");
		}
	}

	@Log("菜单权限-更新的操作")
	@Permission(name = "sys:menu:edit")
	@PostMapping("/update")
	@ResponseBody
	public R update(MenuDO menu) {
		
		MenuDO oldMenuDO = menuService.get(menu.getMenuId());
		if(oldMenuDO!=null) {
			if (!"0".equals(oldMenuDO.getParentId()) && "0".equals(menu.getParentId())) {
				return R.error(1, "不能非法修改当前权限为系统级别权限！");
			}
		}
		
		if (menuService.update(menu) > 0) {
			return R.ok();
		} else {
			return R.error(1, "更新失败");
		}
	}

	@Log("菜单权限-删除操作")
	@Permission(name = "sys:menu:remove")
	@PostMapping("/remove")
	@ResponseBody
	public R remove(String id) {
		if (menuService.remove(id) > 0) {
			return R.ok();
		} else {
			return R.error(1, "删除失败");
		}
	}

	@Log("菜单权限-获取菜单树列表")
	@GetMapping("/tree")
	@ResponseBody
	public Tree<MenuDO> tree() {
		Tree<MenuDO> tree = menuService.getTree();
		return tree;
	}

	@Log("菜单权限-根据角色id获取对应权限")
	@GetMapping("/tree/{roleId}")
	@ResponseBody
	public Tree<MenuDO> tree(@PathVariable("roleId") String roleId) {
		Tree<MenuDO> tree = menuService.getTree(roleId);
		return tree;
	}

}
